package syn;

// 不安全买票
public class BuyingTickets implements Runnable{

    // 定义一些票
    public static int tickets = 10;
    boolean flas = true;
    @Override
    public void run() {
        // 买票
        while (flas){
            buy();
        }
    }

    public synchronized void buy(){
        if (tickets<=0){
            flas = false;
            return;
        }
        /*try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*/
        System.out.println(Thread.currentThread().getName()+"买到了第"+tickets--+"票");
    }

    public static void main(String[] args) {
        BuyingTickets buyingTickets = new BuyingTickets();
        new Thread(buyingTickets,"黄牛").start();
        new Thread(buyingTickets,"小白").start();
        new Thread(buyingTickets,"快递").start();
        new Thread(buyingTickets,"我").start();
    }
}
